//
//  ViewController.swift
//  SHUnitView
//
//  Created by 石虎 on 2020/9/22.
//  Copyright © 2020 石虎. All rights reserved.
// //

import UIKit

class ViewController: UIViewController {

    public var totleTradView: UIView?
    public var orderNumView: UIView?
    public var couponAmountView: UIView?
    public var aiAmountView: UIView?
    
    
    var unitView: SHUnitView?
    var unitView1: SHUnitView?
    var unitView2: SHUnitView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        
        loadSubViews()
        loadSubViewss()
        loadSubViewsss()
        loadSubViewssss()
    }
}

extension ViewController {
    
    fileprivate func loadSubViews() {
        
        let contenerView = SHUnitView()
        contenerView.countOfColumn = 4
        contenerView.spacing = 0.1
        contenerView.aspectRatio = 0.3
        contenerView.backgroundColor = UIColor.yellow
        contenerView.contentEdge = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        view.addSubview(contenerView)
        self.unitView = contenerView
        contenerView.snp.makeConstraints { (maker) in
            maker.left.right.equalToSuperview()
            maker.height.equalTo(20)
            maker.top.equalTo(35)
        }
        
        let array = [
            loadItemView(title: "周一", icon: "content_icon_unchose", selector: #selector(clickSelectorDateTime(sender:)), senderTag: 1000),
            loadItemView(title: "周二", icon: "content_icon_unchose", selector: #selector(clickSelectorDateTime(sender:)), senderTag: 1001),
            loadItemView(title: "周三", icon: "content_icon_unchose", selector: #selector(clickSelectorDateTime(sender:)), senderTag: 1002),
            loadItemView(title: "周四", icon: "content_icon_unchose", selector: #selector(clickSelectorDateTime(sender:)), senderTag: 1003),
            loadItemView(title: "周五", icon: "content_icon_unchose", selector: #selector(clickSelectorDateTime(sender:)), senderTag: 1004),
            loadItemView(title: "周六", icon: "content_icon_unchose", selector: #selector(clickSelectorDateTime(sender:)), senderTag: 1005),
            loadItemView(title: "周日", icon: "content_icon_unchose", selector: #selector(clickSelectorDateTime(sender:)), senderTag: 1006),
        ]
        contenerView.addArrangedSubview(array)
    }
    
    fileprivate func loadItemView(title:String,icon:String,selector:Selector,senderTag:Int) -> UIButton {
        
        let button = UIButton()
        let imageView = UIImageView(image: UIImage(named: icon))
        button.addSubview(imageView)
        let titleLabel = UILabel()
        titleLabel.text = title
        button.addSubview(titleLabel)
        button.tag = senderTag
        let sizeTitleLabel = titleLabel.sizeThatFits(CGSize(width: Double(MAXFLOAT), height:0.0 ))
        imageView.snp.makeConstraints { (maker) in
            maker.top.equalTo(sizeTitleLabel.height/2)
            maker.left.equalTo(20)
            maker.width.height.equalTo(14)
            maker.centerY.equalTo(titleLabel.center.y)
        }
        titleLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(imageView.snp.right).offset(3)
            maker.top.equalTo(0)
            maker.width.equalTo(60)
            maker.height.equalToSuperview()
            maker.centerY.equalTo(imageView.center.y)
        }
        //view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: selector))
        button.addTarget(self, action: selector, for: .touchUpInside)
        return button
    }
}

extension ViewController {
    
    fileprivate func loadSubViewss() {

        let contenerView = SHUnitView()
        contenerView.countOfColumn = 5
        contenerView.backgroundColor = UIColor.orange
        contenerView.contentEdge = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
        view.addSubview(contenerView)
        contenerView.snp.makeConstraints { (maker) in
            maker.left.right.equalToSuperview()
            maker.height.equalTo(20)
            maker.top.equalTo(unitView!.snp.bottom).offset(10)
        }
        self.unitView1 = contenerView
        
        let view1 = loadItemView(title: "朴新网校", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view1.backgroundColor = .yellow
        
        let view2 = loadItemView(title: "朴新助教", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view2.backgroundColor = .magenta
        
        let view3 = loadItemView(title: "数学宝典", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view3.backgroundColor = .purple
        
        let view4 = loadItemView(title: "家校优学", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view4.backgroundColor = .blue
        
        let view5 = loadItemView(title: "我的团队", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view5.backgroundColor = .magenta
        
        let view6 = loadItemView(title: "我的服务", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view6.backgroundColor = .purple
        
        let view7 = loadItemView(title: "新增swift", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view7.backgroundColor = .blue
        
        let view8 = loadItemView(title: "电子书swift", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view8.backgroundColor = .magenta
        
        let view9 = loadItemView(title: "swift签约", icon: "menu_icon_coupon", selector: #selector(showTradingCenterView))
        view9.backgroundColor = .yellow
        
        let array = [
            view1,
            view2,
            view3,
            view4,
            view5,
            view6,
            view7,
            view8,
            view9,
        ]
        contenerView.addArrangedSubview(array)
    }
    
    fileprivate func loadItemView(title: String, icon: String, selector: Selector) -> UIView {
        let view = UIView()
        let imageView = UIImageView(image: UIImage(named: icon))
        view.addSubview(imageView)
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.font = .systemFont(ofSize: 12)
        view.addSubview(titleLabel)
        imageView.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.centerY.equalToSuperview().offset(-10)
        }
        titleLabel.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.top.equalTo(imageView.snp.bottom).offset(10)
        }
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: selector))
        return view
    }
}

extension ViewController {
    
    func loadSubViewsss() {
        
        let unitView = SHUnitView()
        unitView.countOfColumn = 2
        unitView.aspectRatio = 0.5
        unitView.backgroundColor = UIColor.magenta
        view.addSubview(unitView)
        unitView2 = unitView
        unitView.snp.makeConstraints { (maker) in
            maker.left.right.equalToSuperview()
            maker.height.equalTo(20)
            maker.top.equalTo(unitView1!.snp.bottom).offset(10)
        }
        
        totleTradView = loadItemView(topTitle: "0.00", bottomTitle: "今日成交金额(元)", selector: nil)
        totleTradView?.backgroundColor = .red
        orderNumView  = loadItemView(topTitle: "0.00", bottomTitle: "今日成交个数(元)", selector: nil)
        orderNumView?.backgroundColor = .orange
        couponAmountView = loadItemView(topTitle: "0.00", bottomTitle: "今日报名总金额(元)", selector: nil)
        couponAmountView?.backgroundColor = .yellow
        
        let array = [totleTradView!,orderNumView!,couponAmountView!]
        unitView.addArrangedSubview(array)
        
    }
    
    public func loadItemView(topTitle:String,bottomTitle:String,selector:Selector?) -> HomeDataView {
        let view = HomeDataView()
        view.topLabel.text = topTitle
        
        view.bottomLabel.text = bottomTitle
        let value = ViewController.ts_adjustFontSize(ofSize: 13)
        print("[][][]=== \(value)")
        view.bottomLabel.font = .systemFont(ofSize: value)
        
        if selector != nil {
            view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: selector!))
        }
        return view
        
    }
}

extension ViewController {
    
    func loadSubViewssss() {
        
        let unitView = SHUnitView()
        unitView.countOfColumn = 1
        unitView.aspectRatio = 0.5
        unitView.backgroundColor = UIColor.magenta
        view.addSubview(unitView)
        unitView.snp.makeConstraints { (maker) in
            maker.left.right.equalToSuperview()
            maker.height.equalTo(20)
            maker.top.equalTo(unitView2!.snp.bottom).offset(10)
        }
        
        let view = loadItemView1(title: "朴新助教", icon: "大学首页7月15日备份 3", selector: nil)
        
        let array = [view]
        unitView.addArrangedSubview(array)
        
    }
    
    fileprivate func loadItemView1(title:String,icon:String,selector:Selector?) -> UIView {
        let view = UIView()
        let imageView = UIImageView(image: UIImage(named: icon))
        view.addSubview(imageView)
        let titleLabel = UILabel()
        let value = ViewController.ts_adjustFontSize(ofSize: 13)

        titleLabel.font = .systemFont(ofSize: value)
        titleLabel.text = title
        view.addSubview(titleLabel)
        imageView.snp.makeConstraints { (maker) in
            maker.top.left.right.equalToSuperview()
            maker.bottom.equalToSuperview().offset(-20)
        }
        titleLabel.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.top.equalTo(imageView.snp.bottom).offset(2)
            
        }
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: selector))
        return view
    }
}


extension ViewController {
    
    @objc func clickSelectorDateTime(sender:UIButton) {
        sender.isSelected = !sender.isSelected
        if sender.tag == 1000 {
            print("=========我是周一111")
        } else if sender.tag == 1001 {
            print("=========我是周一")
        } else if sender.tag == 1002 {
            print("=========我是周2")
        } else if sender.tag == 1003 {
            print("=========我是周3")
        } else if sender.tag == 1004 {
            print("=========我是周4")
        } else if sender.tag == 1005 {
            print("=========我是周5")
        } else if sender.tag == 1006 {
            print("=========我是周6")
        }
    }
    
    @objc func showTradingCenterView() {
        print("=========showTradingCenterView")
    }
    
    /// 适配下字体大小
    /// - Parameter ofSize: 设计图尺寸的字体大小
    /// - Returns: 当前屏幕应该显示的字体大小
    open class func ts_adjustFontSize(ofSize:CGFloat) -> CGFloat {
        //当前设计图的尺寸是以 XS 设计的。即屏幕宽度为 375
        return ofSize * UIScreen.main.bounds.width / 375;
    }

}
